package controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import common.BaseServlet;
import common.CartShop;
import service.UserDAO;
import bean.User;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/Register")
public class RegisterServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       UserDAO uDao = new UserDAO();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    public String basefun(HttpServletRequest request, HttpServletResponse response) {
		return "f:jsp/Register";
	}
    
    //手机号注册
    public void registerByTel(HttpServletRequest request, HttpServletResponse response) {
    	//通过手机注册
    	String tel = request.getParameter("userTel");	//userTel是前端页面用户输入的手机号，也在jsp页面
    	String psw = request.getParameter("userPassword");
    	PrintWriter out;
    	try {
			out = response.getWriter();
			if(uDao.queryByTel(tel)!=null) {
				out.print("用户已存在");
	    	}else {
	    		//注册成功后将user对象存入session中，修改ifLogin为1，
				//并将注册之前的购物车和数据库中用户的购物车进行合并
		    	uDao.addUserByTel(tel, psw);
		    	HttpSession session=request.getSession();
		    	session.setMaxInactiveInterval(3600);
		    	User user=uDao.queryByTel(tel);
		    	session.setAttribute("user", user);
		    	session.setAttribute("ifLogin", 1);
		    	//将购物车合并
				CartShop cartShop=(CartShop) session.getAttribute("cartShop");
				cartShop.LoginCartShop(user.getUserid());
				session.setAttribute("cartShop", cartShop);
				//根据判断购物车为空存入ifEmpty
				session.setAttribute("ifEmpty",cartShop.ifEmpty());
		    	
				out.print("ok");
	    	}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    }
    
  //邮箱注册
    public void registerByMail(HttpServletRequest request, HttpServletResponse response) {
    	//通过邮箱注册
    	String tel = request.getParameter("userTel");
    	String psw = request.getParameter("userPassword");
    	String mail = request.getParameter("userMail");
    	String pswByMail = request.getParameter("userPswByMail");
    	String telByMail = request.getParameter("userTelByMail");
    	uDao.addUserByMail(mail, pswByMail,telByMail);
    	
    	PrintWriter out;
    	try {
			out = response.getWriter();
			if(uDao.queryByTel(tel)!=null) {
				out.print("用户已存在");
	    	}else {
		    	uDao.addUserByTel(tel, psw);
				out.print("ok");
	    	}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    }

}
